// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/exec/window_build.h>

namespace kumo::pollux::exec {
    /// The PartitionStreamingWindowBuild is used when the input data is already
    /// sorted by {partition keys + order by keys}. The logic identifies partition
    /// changes when receiving input rows and splits out WindowPartitions for the
    /// Window operator to process.
    class PartitionStreamingWindowBuild : public WindowBuild {
    public:
        PartitionStreamingWindowBuild(
            const std::shared_ptr<const core::WindowNode> &windowNode,
            pollux::memory::MemoryPool *pool,
            const common::SpillConfig *spillConfig,
            tsan_atomic<bool> *nonReclaimableSection);

        void addInput(RowVectorPtr input) override;

        void spill() override {
            POLLUX_UNREACHABLE();
        }

        std::optional<common::SpillStats> spilledStats() const override {
            return std::nullopt;
        }

        void noMoreInput() override;

        bool hasNextPartition() override;

        std::shared_ptr<WindowPartition> nextPartition() override;

        bool needsInput() override {
            // No partitions are available or the currentPartition is the last available
            // one, so can consume input rows.
            return partitionStartRows_.empty() ||
                   currentPartition_ == partitionStartRows_.size() - 2;
        }

    private:
        void buildNextPartition();

        // Vector of pointers to each input row in the data_ RowContainer.
        // Rows are erased from data_ when they are output from the
        // Window operator.
        std::vector<char *> sortedRows_;

        // Holds input rows within the current partition.
        std::vector<char *> inputRows_;

        // Indices of  the start row (in sortedRows_) of each partition in
        // the RowContainer data_. This auxiliary structure helps demarcate
        // partitions.
        std::vector<vector_size_t> partitionStartRows_;

        // Used to compare rows based on partitionKeys.
        char *previousRow_ = nullptr;

        // Current partition being output. Used to construct WindowPartitions
        // during resetPartition.
        vector_size_t currentPartition_ = -1;
    };
} // namespace kumo::pollux::exec
